<?php
declare(strict_types=1);

namespace App\Utils;

use League\Flysystem\Filesystem;
use Hyperf\Di\Annotation\Inject;
use App\Dao\Filemd5Dao;
use Swlib\SaberGM;
use App\Constants\ErrorCode;

/**
 * @Description 上传接口
 * @author abell
 * @since 2020-04-15
 */
class UploadUtil
{
    /**
     * @Inject
     * @var Filesystem
     */
    private $filesystem;

    private $web_img_path = '/img/';

    /** 使用swoole静态资源上传
     * @author abell
     * @since 2020-05-06
     */
    public function upload_img($file){
        if($file->getRealPath()){
            $img_md5 = md5_file($file->getRealPath());
            $has_file = make(Filemd5Dao::class)->hasFile($img_md5);
            if($has_file){
                return ['code'=>ErrorCode::SUCCESS,'msg'=>'直接调用','imgpath'=>config('web.web_host').$this->web_img_path.$has_file];
            }else{
                // 上传图片
                $save_file = self::swoole_file($file,$img_md5);
                if($save_file['save_file']){
                    return  ['code'=>ErrorCode::SUCCESS,'msg'=>'上传成功','imgpath'=>config('web.web_host').$save_file['img']];
                }else{return  ['code'=>ErrorCode::ERR_BUESSUS,'msg'=>'上传错误','imgpath'=>''];}
            }
        }else{
            return  ['code'=>ErrorCode::ERR_BUESSUS,'msg'=>'参数错误','imgpath'=>''];
        }
    }

    /** swoole通用图片上传
     * @author abell
     * @since 2020-05-06
     */
    public function swoole_file($file,$img_md5){
        $stream = fopen($file->getRealPath(), 'r+');
        $filePath = config('server.settings.document_root').$this->web_img_path.date("Ymd") . "/" ;
        !is_dir($filePath)&&@mkdir($filePath,0777,true);
        $fileName = uniqid().'.'.$file->getExtension();
        $save_path = substr($this->web_img_path,1).date("Ymd") . "/".$fileName;
        $this->filesystem->writeStream(
            $save_path, $stream
        );
        // 写入数据库
        $save_file = make(Filemd5Dao::class)->saveFile([
            'md5'=>$img_md5,
            'name'=>'上传图片',
            'content'=>date("Ymd") . "/".$fileName,
            'add_time' =>time(),
            'update_time' =>time(),
            'type' => 1
        ]);
        fclose($stream);
        return ['save_file'=>$save_file,'img'=>"/".$save_path];
    }

    /** 远程图片下载
     * @author abell
     * @since 2020-04-27
     */
    public function download_img($url=''){
        if(empty($url)){
            return false;
        }else{
            // 校验远程图片的md5,查看本地是否已经存在
            $img_md5 = md5_file($url);
            $has_file = make(Filemd5Dao::class)->hasFile($img_md5);
            if($has_file){
                return ['code'=>ErrorCode::SUCCESS,'imgpath'=>config('web.web_host').$this->web_img_path.$has_file];
            }else{
                $fileName=uniqid().'.jpg';
                $filePath = config('server.settings.document_root').$this->web_img_path.date("Ymd") . "/" ;
                !is_dir($filePath)&&@mkdir($filePath,0777,true);
                $save_path = $filePath.$fileName;
                $reponse = SaberGM::download($url,$save_path);
                if($reponse->success){
                    $img_url = config('web.web_host').$this->web_img_path.date("Ymd") . "/".$fileName;
                    return ['code'=>ErrorCode::SUCCESS,'imgpath'=>$img_url];
                }else{
                    return ['code'=>ErrorCode::ERR_VALIDATION,'imgpath'=>''];
                }
            }
        }
    }

    // 上传文档和文件等数据返回绝对路径
    public function upload_file($file){
        if($file->getRealPath()){
            $img_md5 = md5_file($file->getRealPath());
            $has_file = make(Filemd5Dao::class)->hasFile($img_md5);
            if($has_file){
                return 'excel/'.$has_file;
            }else{
                // 上传文件
                $stream = fopen($file->getRealPath(), 'r+');
                $filePath = config('server.settings.document_root').'/excel/'.date("Ymd") . "/" ;
                !is_dir($filePath)&&@mkdir($filePath,0777,true);
                $fileName = uniqid().'.'.$file->getExtension();
                $this->filesystem->writeStream(
                    $filePath.$fileName,
                    $stream
                );
                // 写入数据库
                $save_file = make(Filemd5Dao::class)->saveFile([
                    'md5'=>$img_md5,
                    'name'=>$fileName,
                    'content'=>date("Ymd") . "/".$fileName,
                    'add_time' =>time(),
                    'update_time' =>time(),
                    'type' => 1
                ]);
                fclose($stream);
                return $filePath.$fileName;
            }
        }else{
            return  ['code'=>ErrorCode::ERR_BUESSUS,'msg'=>'参数错误'];
        }
    }
}